package com.hs.mod_archive.fragment

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.lifecycleScope
import com.hs.framework.base.BaseDataBindFragment
import com.hs.mod_archive.R
import com.hs.mod_archive.databinding.DialogLogDetailBinding
import com.hs.mod_archive.databinding.FragmentAlarmManagementBinding
import com.hs.mod_archive.helper.AlarmManagerHelper
import com.hs.mod_archive.helper.ListViewHelper
import com.hs.mod_archive.helper.ListViewHelper.Companion.perPageCount
import com.hs.mod_archive.view.DialogUtil
import com.hs.mod_archive.view.MyDialog
import com.hs.room.model.archive.AlarmLog
import com.hs.room.repository.service.ILogRepository
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class FragmentAlarmManagement :
    BaseDataBindFragment<FragmentAlarmManagementBinding>() {

    @Inject
    lateinit var service: ILogRepository

    private var types: List<AlarmLog>? = null
    var mDialog: MyDialog? = null
    lateinit var helper: AlarmManagerHelper
    var dialogLogDetailBinding: DialogLogDetailBinding? = null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        helper =
            AlarmManagerHelper(mBinding!!.footPageListView, object :
                AlarmManagerHelper.OnItemClickListener {
                override fun onItemClick(entity: AlarmLog?, position: Int) {
                    showDialogEdit(entity)
                }
            }, object : ListViewHelper.FootSearchCallback {
                override fun onActionSearch(index: Int) {
                    goPage(index)
                }
            })
    }


    private fun goPage(i: Int) {
        lifecycleScope.launch {
            service.getAlarmLog(i, perPageCount).collect {
                types = it
                helper.setListDataAndRefresh(it)
            }
        }
    }


    override fun initData() {
        super.initData()
        lifecycleScope.launch {
            val count = service.countAlarmLog()
            helper.setFootValue(count, 0)
        }
        goPage(0)
    }

    private fun showDialogEdit(mArchiveLevel: AlarmLog?) {

        //-------------------------
        if (null == dialogLogDetailBinding) {
            dialogLogDetailBinding = DialogLogDetailBinding.inflate(layoutInflater)
        }
        dialogLogDetailBinding!!.tvTitle.setText(R.string.addLevel)
        dialogLogDetailBinding!!.btSure.setOnClickListener { _ ->
            dismissDialogEdit()
        }
        val root: ConstraintLayout = dialogLogDetailBinding!!.getRoot()
        val parent = root.parent
        if (null != parent) {
            val viewGroup = parent as ViewGroup
            viewGroup.removeView(root)
            dialogLogDetailBinding!!.etFileName.setText("")
            dialogLogDetailBinding!!.etUserName.setText("")
        }
        if (null != mArchiveLevel) {
        }
        dialogLogDetailBinding!!.tvTitle.setText(if (null != mArchiveLevel) R.string.edit else R.string.add)
        dialogLogDetailBinding!!.btClose.setOnClickListener { _ -> dismissDialogEdit() }
        //-------------------------
        val w = resources.getDimensionPixelSize(R.dimen.dp_582)
        val h = resources.getDimensionPixelSize(R.dimen.dp_382)
        mDialog = DialogUtil.buildSpecialView(activity, root, w, h, false, false)
    }

    fun dismissDialogEdit() {
        mDialog?.dismiss()
        mDialog = null
        dialogLogDetailBinding = null
    }


    override fun onDestroy() {
        super.onDestroy()
        helper.onDestroyView()
    }
}